/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package tools;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Mika
 */
public class SingleThreadMultiplier {

    public static final int MATRIX_NO = 16;
    int[][] product;

    public static void main(String[] args) {
        long startTime = System.currentTimeMillis();
        //--
        SingleThreadMultiplier stm = new SingleThreadMultiplier();
        stm.calculate();
        //stm.calculateWithSingleThread();
        int[][] result = stm.getProduct();
        //System.out.println("Final product: "+result);
        //Matrix.mprint(result);
        //--
        long endTime = System.currentTimeMillis();
        System.out.println("Execution took: " + (endTime - startTime) + " ms");
        
        System.out.print("Print final matrix? ");
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String answer = null;
        try {
            answer = br.readLine();
        } catch (IOException ioe) {
            System.out.println("IO error trying to read your answer");
            System.exit(-1);
        }
        if(answer.equalsIgnoreCase("yes") || answer.equalsIgnoreCase("y")) {
            Matrix.mprint(result);
        }
    }

    public void calculate() {
        MatrixReader mr = new MatrixReader();
        mr.readAllMatrices(MATRIX_NO);
        int[][] m1 = mr.getNextMatrix();
        int[][] m2 = mr.getNextMatrix();
        int[][] product = Matrix.multiply(m1, m2);
        System.out.println("Calculated product 1");
        int count = 1;
        for (int i = 0; i < MATRIX_NO - 2; i++) {
            m1 = mr.getNextMatrix();
            product = Matrix.multiply(product, m1);
            count++;
            System.out.println("Calculated product " + count);
        }
        this.product = product;
    }

    public int[][] getProduct() {
        return product;
    }
    
    public void calculateWithSingleThread() {
        MatrixReader mr = new MatrixReader();
        mr.readAllMatrices(MATRIX_NO);
        
        MatrixThread st;
        Thread t;
        int[][] m1 = mr.getNextMatrix();
        int[][] m2 = mr.getNextMatrix();
        t = new Thread(st = new MatrixThread(m1, m2, "single-thread"));
        t.start();
        try {
            t.join();
        } catch (InterruptedException ex) {
            Logger.getLogger(SingleThreadMultiplier.class.getName()).log(Level.SEVERE, null, ex);
        }
        System.out.println("Calculated product 1");
        int count = 1;
        int[][] product = st.getProduct();

        for (int i = 0; i < MATRIX_NO - 2; i++) {
            m1 = mr.getNextMatrix();
            t = new Thread(st = new MatrixThread(product, m1, "single-thread"));
            t.start();
            try {

                t.join();
            } catch (InterruptedException ex) {
                Logger.getLogger(SingleThreadMultiplier.class.getName()).log(Level.SEVERE, null, ex);
            }
            
            product = st.getProduct();
            count++;
            System.out.println("Calculated product " + count);
        }

        this.product = st.getProduct();

    }
    
}
